package thread;

import java.io.*;
import java.net.*;

public class Server {
	
	public static void main(String[] args) {
		
		try {
			
			ServerSocket ss = new ServerSocket(10);
			
			while(true){
				
				try {
					
					System.out.println("Server dang hoat dong");
					Socket s = ss.accept();
					Request q = new Request(s);
					q.start();
					
				} catch (IOException e) {
					System.out.println("Loi ket noi "+e);
				}
			}
			
		} catch (Exception e) {
			System.out.println("Loi tao socket "+e);
		}
	}
}

   class Request extends Thread {
	
	Socket chanel;
	
	public Request(Socket s){
		chanel = s;
	}
	
	public void run(){
		
		try {
			
			DataOutputStream os = new DataOutputStream(chanel.getOutputStream());
			BufferedReader is = new BufferedReader(new InputStreamReader(chanel.getInputStream()));
			
			while(true){
				
				String s = is.readLine();
				int n = Integer.parseInt(s);
				int a = 0 ;
				String st = "";
				
				while(n != 0){
					a = n % 10;
					st +=a;
					n = n / 10;
				}
				
				os.writeBytes(st);
				os.write(13);
				os.write(10);
				os.flush();
				
				if(s.equals("quit")){
					os.writeBytes("quit");
					break;
				}
			}
			chanel.close();
		} catch (IOException e) {
			System.out.println("Loi yeu cau "+e);
		}		
	}
}

